package poker;
import java.io.Serializable;
import java.util.ListIterator;
import java.util.Vector;

public class Deelname implements Serializable
{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -7106980599249608906L;
	public static final int ROYAL_FLUSH = 13;
	public static final int STRAIGHT_FLUSH = 12;
	public static final int FOUR_OF_A_KIND = 11;
	public static final int FULL_HOUSE = 10;
	public static final int FLUSH = 9;
	public static final int STRAIGHT = 8;
	public static final int THREE_OF_A_KIND = 7;
	public static final int TWO_PAIR = 6;
	public static final int ONE_PAIR = 5;
	public static final int HIGH_CARD = 4;
	
	public static int SMALL_BLIND = 5;
	public static int BIG_BLIND = 10;
	
	int inzet = 0; // komt ook door als 0!!!
	int verschil = 0;
	double resultaat;
	public boolean isDealer = false;
	public boolean isAanBeurt = false;
	public boolean smallBlind = false;
	public boolean bigBlind = false;
	Speler speler;
	Vector<Kaart> kaart;
	boolean removeCards = false;
	boolean hasfolded = false;
   
	public void setSpeler(Speler speler){
		this.speler = speler;
	}
	
   public Deelname(Vector<Kaart> kaarten) 
   {
	   kaart = kaarten;
   }
   
   public Deelname(Object s) {
		kaart = new Vector<Kaart>();
		speler = (Speler)s;
	}
   public Deelname(){
	   kaart = new Vector<Kaart>();
	   speler = null;
   }

	public void zetIn(double i) {
		speler.setSaldo(speler.getSaldo() - i);
		this.inzet += i;
	}
   
   public void setKaarten(Vector<Kaart> kaarten)
   {
	   kaart = kaarten;
   }
   
   public boolean findCard(int suit, int value)
   {
	   for(ListIterator<Kaart> i = kaart.listIterator(); i.hasNext(); )
	   {
		   Kaart k = i.next();
		   if(k.getKleur() - 1 == suit && k.getNummer() - 2 == value)
			   return true;
	   }
	   return false;
   }
   
   
   public int[] getPunten() 
   {
	   // Eerst maar eens uitzoeken of de speler fantastische kaarten heeft.
	   
	   boolean[] cards = new boolean[55];
	   int[] suits = new int[4];
	   int[] values = new int[13];
	   
	   boolean[][] suitValues = new boolean[4][13];
	   int[][] suithCards = new int[4][5];
	   
	   int[] rtn = new int[6];
	   
	   int hCard = 0;
	   int shCard = 0;
	   int thCard = 0;
	   int fhCard = 0;
	   int ffhCard = 0;
	   int hTwoCard = 0;
	   int shTwoCard = 0;
	   int hThreeCard = 0;
	   int hFourCard = 0;
	   
	   
	   Kaart k;
	   int suit;
	   int value;

	   for ( ListIterator<Kaart> i = kaart.listIterator(); i.hasNext(); )
	   {
		   k = i.next();
		   suit = k.getKleur() - 1;
		   value = k.getNummer() - 2;
		   
		   //System.out.println("Deelname: suit = " + suit + ", value = " + value);
		   
		   for(int a = 0, c = 0; a < 4; a++, c++)
		   {
			   for(int b = 0; b < 13; b++, c++)
			   {
				   if(suit == a && value == b)
				   {
					   suithCards[a][0] = suithCards[a][1];
					   suithCards[a][1] = suithCards[a][2];
					   suithCards[a][2] = suithCards[a][3];
					   suithCards[a][3] = suithCards[a][4];
					   suithCards[a][4] = b;
					   
					   suitValues[a][b] = true;
					   cards[c] = true;
				   }
			   }
		   }
		   suits[suit]++;
		   values[value]++;
	   }	   
	   
	   for(int c = 0; c < 13; c++)
	   {
		   if(values[c] == 1)
		   {
			   ffhCard = fhCard;
			   fhCard = thCard;
			   thCard = shCard;
			   shCard = hCard;
			   hCard = c + 2;
		   }
		   if(values[c] == 2)
		   {
			   shTwoCard = hTwoCard;
			   hTwoCard = c + 2;
		   }
		   if(values[c] == 3)
			   hThreeCard = c + 2;
		   if(values[c] == 4)
			   hFourCard = c + 2;
	   }
	   // We beginnen bij de top. Royal flush ?
	   
	   for ( int c = 0; c < 4; c++)
	   {
		   if(findCard(c, 12) && findCard(c, 11) && findCard(c, 10) && findCard(c, 9) && findCard(c, 8))
		   {
			   rtn[0] = ROYAL_FLUSH;
			   return rtn;
		   }
	   }
	   
	   // Straight flush
	   
	   for ( int c = 4; c < 52; c++ )
	   {
		   if( c == 14 )
			   c = 18;
		   if( c == 27 )
			   c = 31;
		   if( c == 40 )
			   c = 44;
		   if( cards[c] )
			   if( cards[c - 1] )
				   if( cards[c - 2] )
					   if( cards[c - 3] )
						   if( cards[c - 4] )
						   {
							   rtn[0] = STRAIGHT_FLUSH;
							   rtn[1] = c / 4;
							   return rtn;
						   }
	   }
	   
	   // Four of a kind
	   
	   for( int c = 0; c < 13; c++)
	   {
		   if( values[c] == 4 )
		   {
			   rtn[0] = FOUR_OF_A_KIND;
			   rtn[1] = hFourCard;
			   return rtn;
		   }
	   }
	   
	   // Full House
	   
	   boolean gotTwo = false;
	   boolean gotThree = false;
	   for( int c = 0; c < 13; c++ )
	   {
		   //System.out.println("Deelname: values[" + c + "] = " + values[c]);
		   if( values[c] >= 3 && !gotThree)
		   {
			   //System.out.println("Deelname: gotThree");
			   gotThree = true;
		   } else if (values[c] >= 2)
		   {
			   //System.out.println("Deelname: gotTwo");
			   gotTwo = true;
		   }
	   }
	   
	   if( gotThree && gotTwo )
	   {
		   rtn[0] = FULL_HOUSE;
		   rtn[1] = hThreeCard;
		   rtn[2] = hTwoCard;
		   return rtn;
	   }
	   
	   //Flush 

	   for(int c = 0; c < 4; c++)
	   {
		   if(suits[c] > 4)
		   {
			   rtn[0] = FLUSH;
			   rtn[1] = suithCards[c][0];
			   rtn[2] = suithCards[c][1];
			   rtn[3] = suithCards[c][2];
			   rtn[4] = suithCards[c][3];
			   rtn[5] = suithCards[c][4];
			   return rtn;
		   }
	   }
	   
	   // Straight
	   
	   for (int c = 4; c < 13; c++)
	   {
		   if( values[c] >= 1)
			   if( values[c - 1] >= 1)
				   if( values[c - 2] >= 1)
					   if( values[c - 3] >= 1)
						   if( values[c - 4] >= 1)
						   {
							   rtn[0] = STRAIGHT;
							   rtn[1] = c;
						   }
	   }
	   if(rtn[0] == STRAIGHT)
		   return rtn;
	   
	   // Three of a kind
	   
	   for( int c = 0; c < 13; c++)
	   {
		   if( values[c] == 3 )
		   {
			   rtn[0] = THREE_OF_A_KIND;
			   rtn[1] = values[c];
			   rtn[2] = hCard;
			   rtn[3] = shCard;
			   return rtn;
		   }
	   }
	   
	   // One / Two pair
	   
	   boolean onePair = false;
	   
	   for( int c = 0; c < 13; c++)
	   {
		   if( values[c] == 2 )
		   {
			   if( onePair )
			   {
				   rtn[0] = TWO_PAIR;
				   rtn[1] = hTwoCard;
				   rtn[2] = shTwoCard;
				   rtn[3] = hCard;
				   return rtn;
			   }
			   onePair = true;
		   }
	   }
	   
	   // One Pair / High card
	   
	   if( onePair )
	   {
		   rtn[0] = ONE_PAIR;
		   rtn[1] = hTwoCard;
		   rtn[2] = hCard;
		   rtn[3] = shCard;
		   rtn[4] = thCard;
		   return rtn;
	   }
	   
	   rtn[0] = HIGH_CARD;
	   rtn[1] = hCard;
	   rtn[2] = shCard;
	   rtn[3] = thCard;
	   rtn[4] = fhCard;
	   rtn[5] = ffhCard;
	   return rtn;
   }
   
   public Speler getSpeler() {
		return speler;
	}

	public String toString(){
		return "Deelname("+hashCode()+"): (inzet: "+inzet+", isAanBeurt: " + isAanBeurt + ", isDealer: " + isDealer + ")";
		/*if(kaart.size() > 0)
			return "Deelname(speler: " + speler + ", inzet: " + inzet + ", hand( " + kaart.get(0) + ", " + kaart.get(1) + " ) )";
		else
			return "Deelname(speler: " + speler + ", inzet: " + inzet + ", hand() )";*/
	}

	public void geefBeurt(boolean b) {
		if(b)
		{
			isAanBeurt = true;
		} else {
			isAanBeurt = false;
		}
	}
}
